package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.OrderService;
import com.lanchetech.bean.request.OrderPageReq;
import com.lanchetech.bean.request.OrderPayReq;
import com.lanchetech.bean.response.*;
import com.lanchetech.bean.vo.OrderDataStatisticsVO;
import com.lanchetech.bean.vo.OrderPayVO;
import com.lanchetech.entity.Orders;
import com.lanchetech.service.CommonOrderService;
import com.lanchetech.service.LogisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Api(value = "订单管理接口", tags = "订单管理接口")
@RequestMapping("/api/order")
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    LogisticsService logisticsService;

    @Autowired
    CommonOrderService commonOrderService;

    @ApiOperation("获取订单分页")
    @PreAuthorize("hasAnyAuthority('order.order-list', 'order.merchants-list', 'order.platform-list')")
    @PostMapping("/getOrderPage")
    public ResultData<BasePageResp<OrderResp>> getOrderPage(@RequestBody OrderPageReq req) {
        return orderService.getOrderPage(req);
    }

    @ApiOperation("获取资金业绩利润统计")
    @PostMapping("/getOrderStatistics")
    public ResultData<OrderDataStatisticsVO> getOrderStatistics(@RequestBody OrderPageReq req) {
        return orderService.getOrderStatistics(req);
    }


    @OperatorLog("导出所有订单Excel")
    @ApiOperation(value = "导出所有订单Excel")
    @PreAuthorize("hasAnyAuthority('order.order-list.export.auth', 'order.platform-list.export.auth', 'order.merchants-list.export.auth')")
    @PostMapping("/exportOrderExcel")
    public void exportOrderExcel(@RequestBody OrderPageReq req, HttpServletResponse response) throws IOException {
        orderService.exportOrderExcel(req, response);
    }

    @ApiOperation("获取订单详情")
    @PreAuthorize("hasAuthority('order.order-detail')")
    @GetMapping("/getOrderDetail")
    public ResultData<OrderResp> getOrderDetail(@RequestParam("id") Long id) {
        return orderService.getOrderDetail(id);
    }

    @OperatorLog("订单发货填写快递物流信息")
    @ApiOperation("订单发货填写快递物流信息")
    @PreAuthorize("hasAnyAuthority('order.order-list.edit.auth','order.platform-list.edit.auth', 'order.merchants-list.edit.auth')")
    @PostMapping("/editOrderLogistics")
    public BaseResp editOrderLogistics(@RequestBody Orders orders) {
        return orderService.editOrderLogistics(orders);
    }

    @ApiOperation("获取快递物流信息")
    @GetMapping("/getLogisticsTraces")
    public ResultData<String> getLogisticsTraces(@RequestParam("orderCode") String orderCode, @RequestParam("expCode") String expCode, @RequestParam("expNo") String expNo, @RequestParam(value = "phone",required = false) String phone) {
        try {
            return new ResultData<>(logisticsService.getOrderTracesByJson(orderCode, expCode, expNo, phone));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>("{}");
        }
    }

    @OperatorLog("修改订单备注")
    @ApiOperation("修改订单备注")
    @PreAuthorize("hasAnyAuthority('order.order-list.remark.auth','order.platform-list.remark.auth','order.merchants-list.remark.auth')")
    @PostMapping("/editOrderRemark")
    public BaseResp editOrderRemark(@RequestBody Orders orders) {
        return orderService.editOrderRemark(orders);
    }

    @OperatorLog("关闭订单")
    @ApiOperation("关闭订单")
    @PreAuthorize("hasAnyAuthority('order.order-list.close.auth','order.merchants-list.close.auth', 'order.platform-list.close.auth')")
    @PostMapping("/closeOrder")
    public BaseResp closeOrder(@RequestBody Orders orders) {
        return commonOrderService.closeOrder(orders);
    }

    @ApiOperation(value = "主动关闭订单的退款处理回调【微信支付退款回调】")
    @RequestMapping(value = "/refundNotify", method = {RequestMethod.GET, RequestMethod.POST})
    public String refundNotify(@RequestBody String xmlData) {
        return orderService.refundNotify(xmlData);
    }

    @ApiOperation("代付记录分页")
    @PostMapping("/getAnotherPayOrderPage")
    public ResultData<BasePageResp<OrderPayVO>> getAnotherPayOrderPage(@RequestBody OrderPayReq req) {
        return orderService.getAnotherPayOrderPage(req);
    }


    @ApiOperation("接收物流推送消息")
    @PostMapping("/kdniaoPush")
    public PushLogisticsResp kdniaoPush(@RequestParam Map<String, Object> params) {
        return logisticsService.getPushLogisticsData(params);
    }

    @ApiOperation("订阅物流推送消息")
    @GetMapping("/subscribeKdniaoPush")
    public BaseResp subscribeKdniaoPush(@RequestParam("orderId") Long orderId) {
        return logisticsService.subscribeKdniaoPush(orderId);
    }


}
